import api from '../../api/request'

// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    list: [
      { label: '税收分类编码', field: 'code' },
      { label: '税收分类名称', field: 'categoryName' },
      { label: '开票商品名称', field: 'productName' }
    ],
    data: {
      code: '', categoryName: '', productName: ''
    },
    token: '',
    openId: '',
    inputValue: '',
    unionId: wx.getStorageSync('unionId') || '',
    hasGetToken: false,
    hasUnionId: wx.getStorageSync('unionId')
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  onShow() {
    if (this.data.hasGetToken && !this.token) {
      const { token, openId } = app.globalData.userInfo
      this.getToken({ token, openId })
    }
  },
  // 获取token和openId和userInfo
  getToken({ token, openId }) {
    console.log(112312, token, openId);
    this.setData({
      token, openId, hasGetToken: true
    })
    if (!wx.getStorageSync('unionId')) {
      // this.getUserInfo()
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(2233, res)
        const params = {
          rawData: res.rawData,
          signature: res.signature,
          encryptedData: res.encryptedData,
          iv: res.iv,
          token: this.data.token,
          openId: this.data.openId
        }
        api.post('/api/auth/session/userInfo', params).then(res => {
          console.log(555, res);
          if (res.entity) {
            this.setData({
              unionId: res.entity.weChatUser.unionId,
              hasUnionId: true
            })
            wx.setStorageSync('unionId', res.entity.weChatUser.unionId)
          }
        }).then(() => {
          if (this.data.inputValue) {
            this.handleSearch()
          }
        })
      },
      fail: err => {
        console.log(444, err);
      }
    })
  },
  getUserInfo() {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    wx.getUserInfo({
      withCredentials: true,
      success: res => {
        console.log('getUserInfo, success', res);
        const params = {
          rawData: res.rawData,
          signature: res.signature,
          encryptedData: res.encryptedData,
          iv: res.iv,
          token: this.data.token,
          openId: this.data.openId
        }
        api.post('/api/auth/session/userInfo', params).then(res => {
          if (res.entity) {
            this.setData({
              unionId: res.entity.weChatUser.unionId,
              hasUnionId: true
            })
            wx.setStorageSync('unionId', res.entity.weChatUser.unionId)
          }
        })
        /* wx.request({
          url: 'https://bm.zhushui.net/tax/api/auth/session/userInfo',
          method: 'POST',
          header: {},
          data: params,
          success: res => {
            if (res.code === '10000') {
              this.data.unionId = res.data.unionId
              wx.setStorageSync('unionId', res.data.unionId)
            }
          },
        }) */
      },
      fail: err => {
        console.log('getUserInfo, fail');
      }
    })
  },
  bindKeyInput (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  handleSearch() {
    api.get('/search', { keyword: this.data.inputValue }, { unionId: this.data.unionId }).then(res => {
      console.log(123, res);
      res = res.entity
      if (res.code === 'I0000') {
        this.setData({
          data: {
            code: res.result[0].u_code,
            categoryName: res.result[0].u_short_name,
            productName: res.result[0].u_name
          }
        })
      } else if (res.code === '040102') {
        wx.showToast({ title: '处理异常', icon: 'none' })
      }
      else if (res.code === '099') {
        wx.showToast({ title: '用户key失效或超过次数', icon: 'none' })
      }
      else if (res.code === '098') {
        wx.showToast({ title: '用户签名无效', icon: 'none' })
      }
      else if (res.code === '097') {
        wx.showToast({ title: '输入参数格式不正确', icon: 'none' })
      }
    })
  }
})
